﻿/// <reference path="../vsdoc/jquery-1.8.3-vsdoc.js" />

/*
** Settings for "async" controls, AsyncComboBox, AsyncComboView, AsyncListView
*/

/* SELECT decorated with Submit on Enter press */
function SelectAdjustHandler()
{
    $("SELECT", this).focus();
    $("SELECT", this).keydown(function (e)
    {
        if (e && e.keyCode == 13)
        {
            e.preventDefault();
            $(this).parents("FORM").submit();
        }
    });
    $(".autoSubmit SELECT").change(function ()
    {
        $(this).parents("FORM").submit();
    });
}
/* add autoSubmit class to any SELECT to gain submit on change */
$(document).ready(function ()
{
    $("SELECT.autoSubmit").change(function ()
    {
        $(this).parents("FORM").submit();
    });
});

/* AsyncComboBox */
$(document).ready(function ()
{
    $("BUTTON[name=asyncComboBox]").live('click', function (e)
    {
        e.preventDefault();
        var url = $(this).data("asyncurl") + "&" + new Date().getTime(); // dummy 'random' query string param for IE 6.0
        var parent = $(this).parent();
        parent.load(url, SelectAdjustHandler);
    });
});

/* AsyncList, Async TABLE */
asyncHandlersForEach.push(AppendAsyncListLoader);
function AppendAsyncListLoader()
{
    $("DIV.asyncList").each(function (e)
    {
        var url = $(this).data("jsonurl"); ;
        loadTable(url, $(this), $(this).next());
    });
}

function loadTable(url, target, template)
{
    if (target == null)
    {
        return;
    }
    var queryData = "dt=" + new Date().getTime() // dummy 'random' query
    $.ajax({ async: true
            , url: url
            , type: "GET"
            , dataType: "text" // "text"
            , contentType: "application/json" //"text/plain"
            , data: queryData
            , success: function (persistedJson)
            {
                dataSource = $.parseJSON(persistedJson);

                $(target).bindTemplate(
                {
                    source: dataSource
                    , tagOpen: "{"
                    , tagClose: "}"
                    , template: $(template) || $(target)
                });

                SetTable(target, template);
            }
    });
}


function SetTable(target, template)
{
    $("TABLE CAPTION A[href=#]", target).each(function (e)
    {
        $(this).removeAttr("href");
        $(this).addClass("cursorText");
    });
    $("TABLE CAPTION A[href][href!=#]", target).click(function (e)
    {
        var url = $(this).attr("href");
        if (url == null || url == "") // ie 7--
        {
            return false;
        }
        e.preventDefault();
        loadTable(url, target, template);
        return true;
    });
    $("TABLE TH A", target).each(function ()
    {
        var href = $(this).attr("href");

        if (href != null && href != "")
        {
            $(this).click(function (e)
            {
                e.preventDefault();
                loadTable(href, target, template);
            });
        }
        else
        {
            $(this).addClass("cursorText");
        }
    });
    $("TABLE TD A.doRemove", target).each(function ()
    {
        $(this).click(function (e)
        {
            e.preventDefault();
            if (!confirm($(this).next().attr("value")))
            {
                return false;
            }

            var inputName = $(this).parents("TABLE").data("removeditemname");
            var inputHidden = $(this).siblings("INPUT[type=hidden]").first();

            if (inputName != null && inputName != "" && inputHidden != null)
            {
                inputHidden.attr("name", inputName);
                $(this).parents("TR").hide();
                $(this).parents("FORM").append(inputHidden);
            }
            return false;
        });
    });
}

/* Async TreeView (and any asyncContentToLoad) */
function TabHandler(obj, jQuery)
{
    var $ = jQuery;
    $(".asyncContentToLoad", $(obj)).each(function (e)
    {
        var parent = $(this).parent();
        var url = $(this).attr("value");
        url += url.indexOf("?") >= 0 ? "&" : "?";
        url += new Date().getTime(); // dummy 'random' query string param for IE 6.0
        parent.load(url, HandleAllAsync);
    });
}